package com.ctz.commonframe.utils;

import android.content.Context;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.target.ViewTarget;
import com.ctz.commonframe.R;


/**
 * Created by 陈堂正 on 2017/6/19.
 */
@com.bumptech.glide.annotation.GlideModule
public class CustomGlideModule extends AppGlideModule {
    private static final int CACHE_SIZE_MEMORY = 20 * 1024 * 1024;
    private static final int CACHE_SIZE_DISK = 500 * 1024 * 1024;

    private static String diskCachePath;

    public static String getDiskCachePath(){
        return diskCachePath;
    }

    /**
     *  通过GlideBuilder设置默认的结构(Engine,BitmapPool ,ArrayPool,MemoryCache等等).
     * @param context
     * @param builder
     */
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        ViewTarget.setTagId(R.id.glide_tag_id);
        diskCachePath = FilePathUtil.getCacheFolderPath(context, "ImageCache");
        builder.setDiskCache(new DiskLruCacheFactory(diskCachePath, CACHE_SIZE_DISK));
        builder.setMemoryCache(new LruResourceCache(CACHE_SIZE_MEMORY));
    }

    /**
     * 为App注册一个自定义的String类型的BaseGlideUrlLoader,用于图片文件带数字后缀时解析
     *
     * @param context
     * @param registry
     */
    @Override
    public void registerComponents(Context context, Registry registry) {
//        registry.append(String.class, InputStream.class,new CustomBaseGlideUrlLoader.Factory());
    }

    /**
     * 清单解析的开启
     *
     * @return
     */
    @Override
    public boolean isManifestParsingEnabled() {
        //这里不开启，避免添加相同的modules两次
        return false;
    }
}
